package com.newasia.server.common.tool;

import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * @author LiChong
 * @description redis的缓存
 * @date 2020/4/1
 */

@Configuration
public class MyRedisManager {

    @Autowired(required = false)
    private RedissonClient redissonClient;

    public String checkCache(String key){
        try {
            RBucket rBucket = redissonClient.getBucket(key);
            return rBucket.get().toString();
        }catch (Exception e){
            return null;
        }
    }

    public void setCache(String key,String val,int expire){
        RBucket rBucket = redissonClient.getBucket(key);
        rBucket.set(val);
        rBucket.expire(expire, TimeUnit.MINUTES);
    }

    public void expire(String key,int expire){
        RBucket rBucket = redissonClient.getBucket(key);
        rBucket.expire(expire,TimeUnit.MINUTES);
    }

    public void delete(String key){
        RBucket rBucket = redissonClient.getBucket(key);
        rBucket.delete();
    }
}
